﻿using System;
using Assets.Scripts.GtsScripts;
using SaveDataStructures;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

// Token: 0x0200035A RID: 858
public class SavedScenesManager
{
	// Token: 0x1700037A RID: 890
	// (get) Token: 0x0600162C RID: 5676 RVA: 0x000901A2 File Offset: 0x0008E3A2
	public static SavedScenesManager Instance
	{
		get
		{
			if (SavedScenesManager._instance == null)
			{
				SavedScenesManager._instance = new SavedScenesManager();
			}
			return SavedScenesManager._instance;
		}
	}

	// Token: 0x0600162D RID: 5677 RVA: 0x000901BD File Offset: 0x0008E3BD
	public void QuickSaveScene()
	{
		Debug.Log("Saving Scene");
		this.SaveScene(this.GetQuickSaveName());
	}

	// Token: 0x0600162E RID: 5678 RVA: 0x000901D5 File Offset: 0x0008E3D5
	private string GetQuickSaveName()
	{
		return "quicksave_" + this.GetCurrentScene() + ".json";
	}

	// Token: 0x0600162F RID: 5679 RVA: 0x000901EC File Offset: 0x0008E3EC
	public void SaveScene(string filename)
	{
		SaveData obj = new SaveData(true);
		string content = JsonUtility.ToJson(obj, true);
		IOManager.Instance.SaveFile(filename, content);
	}

	// Token: 0x06001630 RID: 5680 RVA: 0x00090214 File Offset: 0x0008E414
	public void QuickLoadScene()
	{
		Debug.Log("Loading Scene");
		SaveData saveData = this.LoadSaveData(this.GetQuickSaveName());
		this.LoadScene(saveData.scene);
	}

	// Token: 0x06001631 RID: 5681 RVA: 0x00090244 File Offset: 0x0008E444
	public SaveData LoadSaveData(string filename)
	{
		string text = IOManager.Instance.LoadFile(filename);
		if (text == null)
		{
			Debug.LogError("Load scene has failed.");
			return null;
		}
		return JsonUtility.FromJson<SaveData>(text);
	}

	// Token: 0x06001632 RID: 5682 RVA: 0x00090278 File Offset: 0x0008E478
	public string GetCurrentScene()
	{
		return SceneManager.GetActiveScene().name;
	}

	// Token: 0x06001633 RID: 5683 RVA: 0x00090294 File Offset: 0x0008E494
	public void ReBuildScene()
	{
		Debug.Log("Loading scene contents");
		string json = IOManager.Instance.LoadCachedDataFile();
		SaveData data = JsonUtility.FromJson<SaveData>(json);
		this.LoadEntitiesOnScene(data);
	}

	// Token: 0x06001634 RID: 5684 RVA: 0x000902C4 File Offset: 0x0008E4C4
	public void LoadScene(string sceneName)
	{
		if (GameController.Instance != null)
		{
			GameController.Instance.SetPausedState(false);
			NetworkManager.singleton.StopHost();
		}
		SceneLoader sceneLoader = UnityEngine.Object.FindObjectOfType<SceneLoader>();
		if (sceneLoader != null)
		{
			sceneLoader.LoadSceneByName(sceneName);
		}
		else
		{
			SceneManager.LoadScene(sceneName);
		}
	}

	// Token: 0x06001635 RID: 5685 RVA: 0x0009031C File Offset: 0x0008E51C
	public void RestartScene()
	{
		string currentScene = this.GetCurrentScene();
		this.LoadScene(currentScene);
	}

	// Token: 0x06001636 RID: 5686 RVA: 0x00090337 File Offset: 0x0008E537
	private void LoadEntitiesOnScene(SaveData data)
	{
		this.savedata = data;
		this.LoadObjects(data.entities);
		this.LoadGiantess(data.entities);
	}

	// Token: 0x06001637 RID: 5687 RVA: 0x00090358 File Offset: 0x0008E558
	private void LoadObjects(EntitiesData data)
	{
		for (int i = 0; i < data.objects.Length; i++)
		{
			EntityData entityData = data.objects[i];
			Debug.Log("Loading... " + entityData.name);
			string name = entityData.name;
			int id = entityData.id;
			Vector3 position = entityData.position;
			Quaternion rotation = Quaternion.Euler(entityData.rotation);
			float y = entityData.scale.y;
			ClientPlayer.Instance.CallCmdSpawnObject(name, position, rotation, y, id);
		}
	}

	// Token: 0x06001638 RID: 5688 RVA: 0x000903E0 File Offset: 0x0008E5E0
	private void LoadGiantess(EntitiesData data)
	{
		for (int i = 0; i < data.giantess.Length; i++)
		{
			GiantessData giantessData = data.giantess[i];
			Debug.Log("Loading... " + giantessData.name);
			string name = giantessData.name;
			int id = giantessData.id;
			Vector3 position = giantessData.position;
			Quaternion rotation = Quaternion.Euler(giantessData.rotation);
			float y = giantessData.scale.y;
			ClientPlayer.Instance.CallCmdSpawnGiantess(name, position, rotation, y, id);
		}
	}

	// Token: 0x06001639 RID: 5689 RVA: 0x00090468 File Offset: 0x0008E668
	public void OnObjectLoaded(EntityBase obj)
	{
		if (this.savedata == null)
		{
			return;
		}
		EntitiesData entities = this.savedata.entities;
		for (int i = 0; i < entities.objects.Length; i++)
		{
			EntityData entityData = entities.objects[i];
			if (entityData.id == obj.id)
			{
				obj.name = entityData.name;
				obj.ChangeOffset(entityData.floorOffset);
				obj.ChangeScale(entityData.scale.y);
				obj.OnPlaced();
				return;
			}
		}
	}

	// Token: 0x0600163A RID: 5690 RVA: 0x000904F0 File Offset: 0x0008E6F0
	public void OnGiantessLoaded(Giantess gts)
	{
		if (this.savedata == null)
		{
			return;
		}
		EntitiesData entities = this.savedata.entities;
		for (int i = 0; i < entities.giantess.Length; i++)
		{
			GiantessData giantessData = entities.giantess[i];
			if (giantessData.id == gts.id)
			{
				gts.name = giantessData.name;
				gts.ChangeOffset(giantessData.floorOffset);
				gts.ChangeScale(giantessData.scale.y);
				gts.useAltShaders = giantessData.useAltShaders;
				gts.useAltTangents = giantessData.useAltTangents;
				if (giantessData.morphs != null && giantessData.morphs.Length > 0)
				{
					for (int j = 0; j < giantessData.morphs.Length; j++)
					{
						MorphData morphData = giantessData.morphs[j];
						gts.SetMorphValue(morphData.index, morphData.value);
					}
				}
				gts.animationManager.PlayAnimation(giantessData.animation.name, giantessData.animation.isPose, true);
				gts.animationManager.ChangeSpeed(giantessData.animation.speed);
				gts.OnPlaced();
				return;
			}
		}
	}

	// Token: 0x040018E9 RID: 6377
	private static SavedScenesManager _instance;

	// Token: 0x040018EA RID: 6378
	private SaveData savedata;
}
